<?php
class AdvancedSearch extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('knowledge/knowledge_model','knowledge');
		$this->load->model('knowledge/knowledgefiles_model','knowledgefiles');
		$this->load->model('approve/approve_model','approve');
		$this->load->model('member/request_con_model','request_con');
		$this->load->model('member/request_model','request');
		$this->load->model('users/users_model','user');
		ob_start();
	}
	
	public function index($type=FALSE,$id=FALSE)
	{
		if(login_data('id') != ""){
			// ไฟล์ที่ผู้อนุญาติให้ดาวน์โหลดได้
			$data['allows'] = $this->request->where("req_user_id = ".login_data('id')." and request.status = 'อนุญาต'")->get();
			
			$data['allow_id'] = array();
			foreach($data['allows'] as $item){
				$data['allow_id'][] = $item['tbknowledge_id'];
			}
		}
		
		//	$this->db->debug = true;
		$this->template->append_metadata(js_datepicker());	
		$data['knowledgetype'] = $this->db->getarray("SELECT * FROM tbknowledgetype order by id ");
		$data['knowledgegroup'] = $this->db->getarray("SELECT * FROM tbknowledgegroup order by id ");
		$data['sectionid'] = $this->db->getarray("SELECT * FROM tbsection order by id");
		
		if($_GET)
		{
	
		 $sql = 'select t.* from tbknowledge as t join tbknowledge_tag as tt on tt.tbknowledge_id = t.id join tags on tags.id= tt.tag_id where 1 ';
		 
			$sql.= @$_GET['search']!='' ? " AND t.title LIKE '%".$_GET['search']."%' ": "";
			$sql.= @$_GET['titlesearch']!='' ? " AND (t.title LIKE '%".$_GET['titlesearch']."%' or t.description LIKE '%".$_GET['titlesearch']."%') ": "";
			$sql.= @$_GET['searchtag']!='' ? " AND tags.tag LIKE '%".$_GET['searchtag']."%' ": "";	
			$sql.= @$_GET['checkbox_search']!='' ? " AND t.knowledgetypeid LIKE '%".$_GET['checkbox_search']."%' ": "";
			$sql.= @$_GET['date1']!='' ? " AND (t.publishdate BETWEEN '".Date2DB($_GET['date1'])."' AND '".Date2DB($_GET['date2'])."')" : "";
			$sql.= @$_GET['knowledgegroup']!='' ? "AND t.knowledgegroupid LIKE'%".$_GET['knowledgegroup']."%' ": "";
			$sql.= @$_GET['knowledgetype']!='' ? "AND t.sectionid LIKE'%".$_GET['knowledgetype']."%' ": "";
			$sql.= @$_GET['Year']!='' ? " AND YEAR(t.publishdate)=".($_GET['Year']-543)."" :"";		
			$sql .= ' group by t.id ';
			$data['dataList'] = $this->knowledge->get($sql);
		}
		
		if($type == 'knowledge')
		{
		 $sql = 'select t.* from tbknowledge as t join tbknowledge_tag as tt on tt.tbknowledge_id = t.id join tags on tags.id= tt.tag_id where 1 ';
		 $sql .= "AND t.knowledgegroupid=".$id;
		 $sql .= ' group by t.id ';
		 $data['id'] = $id;
		 $data['dataList'] = $this->knowledge->get($sql);
		}
		
		elseif($type == 'section')
		{
		 $sql = 'select t.* from tbknowledge as t join tbknowledge_tag as tt on tt.tbknowledge_id = t.id join tags on tags.id= tt.tag_id where 1 ';
		 $sql .= "AND t.sectionid=".$id;
		 $sql .= ' group by t.id ';
		 $data['id'] = $id;
		 $data['dataList'] = $this->knowledge->get($sql);
		}
	 	$data['pagination'] = $this->knowledge->pagination();
		$this->template->build('index',$data);
	}

	public function privatestatus($id=FALSE)
	{
		if(is_login())
		{
			/*	
			$data['pid'] = $id;
			$data['request_by'] = login_data('id'); 
			$data['request_date'] = date("Y-m-d");
			$this->approve->save($data);
			 * 
			 */
			
			$data['detail'] = $this->knowledge->get_row($id);
			$this->template->build('request',$data);
		}else{
			$this->template->build('login');		
		}			
	}

	function send_request(){
		if($_POST){
			$knowledge = $this->knowledge->get_row($_POST['tbknowledge_id']);
			$own_user_id = $this->user->where('email = "'.$knowledge['accredit_email'].'"')->get_row();
			
			$_POST['req_user_id'] = login_data('id');
			$_POST['own_user_id'] = $own_user_id['id'];
			$_POST['created'] = date("Y-m-d H:i:s");
			$_POST['status'] = 'รอการอนุญาต';
			$_POST['request_id'] = $this->request->save($_POST);
			
			$this->request_con->save($_POST);
			
			set_notify('success', 'ส่งคำขอเรียบร้อยแล้ว');
		}
		redirect('home');
	}
	
}
?>
